def can_form_square(matchsticks):
    total_length = sum(matchsticks)

    # 正方形的四条边长度
    if total_length % 4 != 0:
        return False
    
    side_length = total_length // 4
    matchsticks.sort(reverse=True)
    
    sides = [0] * 4  # 四条边的当前长度

    def backtrack(index):
        if index == len(matchsticks):
            return sides[0] == sides[1] == sides[2] == sides[3] == side_length
        
        for i in range(4):
            if sides[i] + matchsticks[index] <= side_length:
                sides[i] += matchsticks[index]
                if backtrack(index + 1):
                    return True
                sides[i] -= matchsticks[index]
        
        return False

    return backtrack(0)

if __name__ == "__main__":
    # 示例
    matchsticks = [1, 1, 2, 2, 2]
    print("能否构成正方形:", can_form_square(matchsticks))